package com.fan.service.provider;

import com.fan.dao.BaseDao;
import com.fan.dao.provider.ProviderDao;
import com.fan.dao.provider.ProviderDaoImpl;
import com.fan.dao.user.UserDao;
import com.fan.dao.user.UserDaoImpl;
import com.fan.pojo.Provider;
import com.fan.pojo.Role;
import com.fan.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{
    private ProviderDao providerDao;
    public ProviderServiceImpl(){
        providerDao = new ProviderDaoImpl();
    }


    @Override
    public int getProviderCount(String proCode, String proName) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = providerDao.getProviderCount(connection,proCode,proName);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    @Override
    public List<Provider> getProviderList(String queryProCode, String queryProName, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<Provider> proList = null;
        System.out.println(queryProCode);
        System.out.println(queryProName);
        System.out.println(currentPageNo);
        System.out.println(pageSize);
        try {
            connection = BaseDao.getConnection();
            proList = providerDao.getProviderList(connection,queryProCode,queryProName,currentPageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return proList;
    }

    @Override
    public boolean addProvider(Provider provider) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = providerDao.addProvider(connection,provider);
            connection.commit();
            if (updateRows > 0){
                flag = true;
            } else {
                System.out.println("add failed!");
            }
        } catch (Exception e){
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public boolean deleteProviderById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (providerDao.deleteProviderById(connection,delId) > 0){
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public Provider getProviderById(String id) {
        Provider provider = null;
        Connection connection =null;
        try {
            connection = BaseDao.getConnection();
            provider = providerDao.getProviderById(connection,id);
        }catch (Exception e){
            e.printStackTrace();
            provider = null;
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return provider;
    }

    @Override
    public List<Provider> getProviderList() {
        Connection connection = null;
        List<Provider> providerList = null;
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        System.out.println("providerList(service)"+providerList);
        return providerList;
    }

    @Override
    public boolean updateProvider(String id, String proCode, String proName, String proDesc, String proContact, String proPhone, String proAddress, String proFax) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            if (providerDao.updateProviderById(connection,id,proCode,proName,proDesc,proContact,proPhone,proAddress,proFax)){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
}
